package com.qc.blogsystemplus.mapper;

import com.qc.blogsystemplus.bean.ArticleBean;
import com.qc.blogsystemplus.entity.Article;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface ArticleMapper {

    @Select("SELECT article.id,title,content,public_time,readtimes,category.category_name,`user`.nickname,`user`.username FROM article,category,`user`\n" +
            "WHERE article.author_userid = `user`.id AND article.category_id = category.id")
    List<Article> getAllArticle();

    @Select("SELECT article.id,title,content,public_time,readtimes,category.category_name,`user`.nickname,`user`.username FROM article,category,`user`  WHERE article.author_userid = `user`.id AND article.category_id = category.id AND category_id = \n" +
            "(\n" +
            "SELECT category.id FROM category WHERE category.category_name = #{category_name}\n" +
            ")")
    List<Article> getArticleByCate(String category_name);

    @Select("SELECT article.id,title,content,public_time,readtimes,category.category_name,`user`.nickname,`user`.username FROM article,category,`user`  WHERE article.author_userid = `user`.id AND article.category_id = category.id AND article.id = #{articleid}")
    Article getArticleByArticleId(int articleid);

    @Insert("INSERT INTO article (title,content,author_userid,category_id,public_time,readtimes) VALUES(#{title},#{content},#{author_userid},#{category_id},#{public_time},#{readtimes})")
    int addArticle(ArticleBean articleBean);

    @Delete("DELETE FROM article WHERE id = #{id}")
    int delArticle(int id);

    @Select("SELECT article.id,title,content,public_time,readtimes,category.category_name,`user`.nickname,`user`.username FROM article,category,`user`\n" +
            "WHERE article.author_userid = `user`.id AND article.category_id = category.id AND author_userid = (SELECT `user`.id FROM `user` WHERE username = #{username})")
    List<Article> getArticleByUser(String username);
}
